package dome4Map;

import java.util.*;

public class Maptest3 {
    public static void main(String[] args) {
        //投票统计案例
        calc();

    }
    public static void calc() {
        List<String> locations=new ArrayList<>();
        String[] names={"嵩山","天津之眼","外滩","长城"};
        Random r=new Random();
        for(int i=0;i<=80;i++) {
            int index=r.nextInt(4);
            locations.add(names[index]);
        }
        System.out.println(locations);
        Map<String,Integer> map=new HashMap<>();
        //遍历80个学生的选择
        for(String location :locations) {
//            if(map.containsKey(location)) {
//                map.put(location,map.get(location)+1);
//            }else {
//                map.put(location,1);
//            }
            //简化代码
            map.put(location,map.containsKey(location)?map.get(location)+1:1);
        }
       // System.out.println(map);
        map.forEach((k,v)-> System.out.println(k+"被选择了"+v+"次"));
    }

}
